package com.baiyang.server.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baiyang.server.dao.system.UserDao;
import com.baiyang.server.model.system.User;
import com.baiyang.server.tools.DataTool;

/**
 * @author XVX
 * @apiNote 用户redis
 */
@Component
public class UserRedis {
	
	private static final String USER_ID = "USER_ID";
	private static final String USER_USERNAME = "USER_USERNAME";
	@Autowired
	private RedisUtil redisUtil = null;
	@Autowired
	private UserDao userDao = null;
	/**
	 * @apiNote根绝用户ID获取用户信息
	 * @param id
	 * @return
	 */
	public User getUserById(String id) {
		if(DataTool.isBlank(id)) {
			return null;
		}
		User user = (User) redisUtil.hget(USER_ID, id);
		if(user == null) {
			user = new User();
			user.setId(id);
			user = userDao.selectOne(user);
			if(user != null) {
				redisUtil.hset(USER_ID, id, user);
			}
		}
		return user;
	}
	
	
	/**
	 * @apiNote根绝用户名获取用户信息
	 * @param id
	 * @return
	 */
	public User getUserByUsername(String username) {
		if(DataTool.isBlank(username)) {
			return null;
		}
		User user = (User) redisUtil.hget(USER_USERNAME, username);
		if(user == null) {
			user = new User();
			user.setUserName(username);
			user = userDao.selectOne(user);
			if(user != null) {
				redisUtil.hset(USER_ID, username, user);
			}
		}
		return user;
	}
	/**
	 * @apiNote 根绝id删除用户信息
	 * @param id
	 */
	public void deleteById(String id) {
		redisUtil.hdel(USER_ID, id);
	}
	
}
